using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using Crainiate.Data.Persistence.Providers;
using Crainiate.Data.Persistence.Providers.SqlServer;
using Crainiate.Data.Persistence.Tools.Properties;

namespace Crainiate.Data.Persistence.Tools
{
    public partial class SqlContextForm : WizardBaseForm
    {
        private SqlContext _context;

        public SqlContextForm()
        {
            InitializeComponent();
            
            //Load the previous paths settings
            string setting = Settings.Default.SqlProviderServers;

            if (setting != null && setting.Length > 0)
            {
                string[] servers = setting.Split("|".ToCharArray());
                foreach (string server in servers)
                {
                    if (server.Length > 0) cboServer.Items.Add(server);
                }

                cboServer.Text = servers[0];
            }

            cboAuthentication.SelectedIndex = 0;

            if (Settings.Default.SqlProviderLastTrusted == "False") cboAuthentication.SelectedIndex = 1;
            txtUsername.Text = Settings.Default.SqlProviderLastUsername;
        }

        public SqlContext Context
        {
            get
            {
                return _context;
            }
        }

        protected override void OnNextClick(object sender, EventArgs e)
        {
            if (cboServer.Text.Trim().Length == 0)
            {
                MessageBox.Show("Select a server to connect to.", "Invalid Server Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //Save the selected server value and any previous values as a delimited string
            string setting = Settings.Default.SqlProviderServers;
            string newSetting = cboServer.Text;

            if (setting.Length > 0)
            {
                string[] servers = setting.Split("|".ToCharArray());
                foreach (string server in servers)
                {
                    if (server != cboServer.Text) newSetting += "|" + server;
                }
            }

            Settings.Default.SqlProviderServers = newSetting;
            Settings.Default.SqlProviderLastTrusted = (cboAuthentication.SelectedIndex == 0) ? "True" : "False" ;
            Settings.Default.SqlProviderLastUsername = txtUsername.Text ;

            Settings.Default.Save();

            //Set up the context
            _context = new SqlContext();
            _context.Server = cboServer.Text;
            _context.Integrated = (cboAuthentication.SelectedIndex == 0);

            if (!_context.Integrated)
            {
                _context.UserId = txtUsername.Text;
                _context.Password = txtPassword.Text;
            }

            //Try get a connection
            try
            {
                SqlConnection connection = _context.GetConnection();
            }
            catch (Exception ex)
            {
                MessageBox.Show("A connection could not be established." + "\r\n\r\n" + ex.Message , "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            base.OnNextClick(sender, e);
        }

        private void cboAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtUsername.Enabled = (cboAuthentication.SelectedIndex == 1);
            txtPassword.Enabled = txtUsername.Enabled;
        }
    }
}